<div class="modal fade" id="modal-default">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="roledialogtitle">添加角色</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="defaultForm">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="role" class="col-sm-2 control-label">角色名</label>

                            <div class="col-sm-9">
                                <input type="hidden" class="form-control" id="roleid">
                                <input type="text" class="form-control" id="role" name="role" placeholder="管理员">
                            </div>
                            <div class="col-sm-1">
                                <span class="promptMsg" id="promptMsg_device_id">*</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">菜单权限</label>
                            <div class="col-sm-10 j-menu-power">

                            </div>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saverole();">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<script>

    function buildHtml(cb){
        $.ajax({
            url:'${request.contextPath}/menu/tree',
            type:'get',
            headers: {
                Accept: "application/json   "
            },
            dataType: "json",
            data: null,
            contentType: 'application/json;charset=utf-8',
            success: function (result){
                cb(result);
            }
        })

    }

    function showadddialog(role)
    {
        buildHtml(function(result){
            var html = "";
            for (var item in result){
                html +='<div class="row">' +
                    '<div class="col-md-12 j-parent-menu"  data-type='+result[item].menu.type+'  data-name='+result[item].menu.name+' data-module='+result[item].menu.module+' data-subModule='+result[item].menu.subModule+' >'+result[item].menu.name+'</div>' ;
                var children = ""
                for(var child in result[item].children){
                    children +=  '<div class="col-md-6 j-child-menu" data-type='+result[item].children[child].type+' data-name='+result[item].children[child].name+' data-module='+result[item].children[child].parentMenu.module+' data-subModule='+result[item].children[child].subModule+' >' +
                        '         <label>' +
                        '            <input type="checkbox" class="minimal">' +result[item].children[child].name+
                        '           </label>'+
                        '        </div>' ;
                }
                html += children;
                html +=   '</div>';
            }
            var htmlObj = $(html);

            var authority = role ? role.authority : "";
            if(authority != ""){
                authority = JSON.parse(authority);
                $.each(authority,function (i,item) {
                    $.each(item.children,function(i,child){
                        var obj = htmlObj.find('div[data-subModule="'+child.subModule+'"]');
                        obj.find('input[type="checkbox"]').attr("checked","checked");
                    })
                })
            }

            $(".j-menu-power").empty().append(htmlObj);
            $('input[type="checkbox"].minimal').iCheck({
                checkboxClass: 'icheckbox_minimal-red',
                radioClass   : 'iradio_minimal-red'
            });
        });
        $("#roleid").val(role? role.id: "-1");
        $("#role").val(role? role.name : "");
        validator();
        if (role)
       	{
        	$("#roledialogtitle").text("编辑角色");
       	}
        else
       	{
        	$("#roledialogtitle").text("添加角色");
       	}
        $("#modal-default").modal('show');
    }

    function validator() {
        $('#defaultForm').bootstrapValidator({
            message: '此值无效',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                role: {
                    validators: {
                        notEmpty: {
                            message: '角色名不能为空'
                        },
                        stringLength: {
                            min: 1,
                            max: 6,
                            message: '角色名必须大于0，长度小于6个字符'
                        }
                    }
                }
            }
        });
    }

    function showeditdialog(id)
    {
        $.ajax({
            url:'${request.contextPath}/role/'+id,
            type:'get',
            headers:{
                Accept:"application/json"
            },
            contentType:'application/json',
            success:function(result){
                showadddialog(result.msg);
            }
        });

    }

    function saverole()
    {
        //获取表单对象
        var bootstrapValidator = $('#defaultForm').data('bootstrapValidator');
        //手动触发验证
        bootstrapValidator.validate();
        if(!bootstrapValidator.isValid()){
            return;
        }
        var rows = $(".j-menu-power").find('div.row');
        var arr = [];
        rows.each(function(i,item){
            if($(item).find('div.checked').length !=0){
                var _map_parent = {};
                _map_parent.name = $(item).find('.j-parent-menu').attr("data-name");
                _map_parent.module = $(item).find('.j-parent-menu').attr("data-module");
                _map_parent.subModule = $(item).find('.j-parent-menu').attr("data-subModule");
                _map_parent.type = $(item).find('.j-parent-menu').attr("data-type");

                var _arr_child = [];
                $(item).find('div.checked').each(function(i,child){
                    var _map_child = {};
                    _map_child.name = $(child).closest('.j-child-menu').attr("data-name");
                    _map_child.module = $(child).closest('.j-child-menu').attr("data-module");
                    _map_child.subModule = $(child).closest('.j-child-menu').attr("data-subModule");
                    _map_child.type = $(child).closest('.j-child-menu').attr("data-type");
                    _arr_child.push(_map_child);
                });
                _map_parent.children = _arr_child;
                arr.push(_map_parent);
            }
        });
        var roleid = $("#roleid").val();
        var role = $("#role").val();
        console.dir(arr);
        if (roleid == -1)
        {
            var data = {name:role,authority:JSON.stringify(arr)};
            $.ajax({
                url : "${request.contextPath}/user/saveRole",
                type : "post",
                data : data,
                success : function(s) {
                    if (s.success) {
                        location.reload();
                    } else {
                        alert(s.msg);
                    }
                }
            });
        }
        else
        {
            var data = {id:roleid,name:role,authority:JSON.stringify(arr)}
            $.ajax({
                url : "${request.contextPath}/user/updateRole",
                type : "post",
                data : data,
                success : function(s) {
                    if (s.success) {
                        location.reload();
                    } else {
                        alert(s.msg);
                    }
                }
            });
        }
    }
</script>
